<?php
namespace app\index\controller;
use think\Controller;
use app\admin\model\Users;
use app\admin\model\Video;
use app\admin\model\Tixian;
use app\admin\model\Yaoqing;
use app\admin\model\Orderlist;
use app\common\controller\Frontend;
class Daili extends Frontend
{   

    private $video;
    private $user;
    private $uid;

    public function _initialize()
    {  
        // echo request()->action();die();
        if(request()->action() != 'login'  &&  request()->action() != 'reg' &&  empty(session('dailiUid')) ){
            $this->error('请登录后操作!','/daili.php/login?url=daili.php');
        }else{
            $this->video = new Video;
            $this->user  = new Users;
            $this->olist  = new Orderlist;
            $this->yq  = new Yaoqing;
            $this->uid   = session('dailiUid');
        }
     
    }
    //账单
    public function orderlist()
    {   
        $res = $this->olist->where('uid',$this->uid)->order('orderlist_id', 'desc')->paginate(10);
        return view('orderlist',[
            'data'=>$res,
            'page'=>$res->render(),
        ]);
    }
    public function index()
    {   
       $sons =  count(  $this->user->getDaili($this->uid));
       $money = Users::get($this->uid) ->money;
       
       return  view('index',[
                    'sons' =>$sons,
                    'money'=>$money,
                    'newfile'=>$this->video->limit(10) ->order('video_id', 'desc')->select()
                ]);
    }
    public function login()
    {
        if($_POST){
            $_POST['user_pwd'] = md5($_POST['user_pwd']);
            $res = Users::where($_POST)->find();
            if($res){
                session('dailiUid',$res->user_id);
                session('uname',$_POST['user_name']);
                session('level',$this->user->getLevel($res->user_id));
                // return ['code'=>0,'msg'=>$this->user->getLevel($res->user_id)];
                return ['code'=>1,'msg'=>'登录成功!'];
            }else{
                return ['code'=>0,'msg'=>'请检查账号密码!'];
            }
        }else{
            return view();
        }
    } 
    public function reg()
    {
        $res= $this->yq->where(['y_no'=>$_GET['y_no'],'y_use'=>'no'])->find();
        if(!$res) return ['code'=>0,'msg'=>'邀请码不存在!'];
        //写入用户表
        $_POST['user_parentid'] = $res->y_uid;
        $_POST['user_reg_time'] = time();
        $_POST['is_daili']      = true;
        $res = Users::create($_POST);
        //邀请码注册
        $this->yq->save(['y_user'=>$res->user_id,'y_use'=>'yes'],['y_no'=>$_GET['y_no']]);

        if($res){
            return ['code'=>1,'msg'=>'注册成功,请妥善保管账号密码!'];
        }else{
            return ['code'=>0,'msg'=>'注册失败!'];
        }



     }
    public function son()
    {   

        $level  = $this->user->getLevel(session('dailiUid'));
        if($level == 1){
            $chou = config('site.l1_chou');
        }else{
            $chou = config('site.l2_chou');
        }

        $res = Users::where('user_parentid',session('dailiUid'))->paginate(10);
        foreach($res as $v){
            $v->money = $this->getFmoney($v->user_id) * $chou;
        };
        return view('son',['data'=>$res]);
    }
    //获取提现的所有钱
    protected function getFmoney($uid)
    {   

        
        $res = Tixian::where(['uid'=>$uid,'tx_status'=>1])->select();
        $num = 0;
        foreach($res as $v){
            $num += $v->tx_money;
        }
        return $num;

    }
    public function logout()
    {
        session('dailiUid',null);
        return '<script>alert("退出成功!");history.go(-1);</script> ';
    }
    public function qrcode()
    {
        return view();
    }
    public function yaoqing()
    {   
        if($_POST){
            $usermoney = Users::get(session('dailiUid'))->money;
            if($usermoney < 100){
                return ['code'=>0,'msg'=>'余额不足!'];
            }

            $msg = session('dailiUid').time();

            $res = $this->user->save(['money'=>$usermoney - 100],['user_id'=>session('dailiUid')]);

            if($res){
                Yaoqing::create([
                    'y_no'=>$msg,
                    'y_uid'=>session('dailiUid'),
                    'y_maketime'=>time(),
                    'y_use'=>'no'
                ]);
                return ['code'=>1,'msg'=>$msg];
            }else{
                return ['code'=>0,'msg'=>'生成失败,请联系客服! '];
            }
        
        }
        return view('yaoqing',[
            'data'=>Yaoqing::where('y_uid',session('dailiUid'))->paginate(10),
        ]);
    }
    // 余额提现
    public function yue()
    {       
        if($_POST){

            $money  = $_POST['money'];
            $level  = $this->user->getLevel(session('dailiUid'));

            $_POST['money'] -=  $money * config('site.admin_c');

            switch ($level) {
                case 2:
                    $_POST['money'] -= $money * config('site.l1_chou');
                    break;
                
                case 3:
                    $_POST['money'] -= $money * config('site.l2_chou');
                    $_POST['money'] -= $money * config('site.l1_chou');
                    break;
                
            }

            $tixian = new Tixian();
            $tixian->uid       = session('dailiUid');
            $tixian->tx_money  = $_POST['money'];
            $tixian->tx_smoney  = $money;
            $tixian->tx_wallet = $_POST['wallet'];
            $tixian->tx_time   = time();
            $res = $tixian->save();
            if($res){
                //扣除用户提现的钱
                $user = Users::get($this->uid);
                $user->money -= $money;
                $res = $user->save(); 

            }

            if($res){
                return ['code'=>1,'msg'=>'申请提现成功!'];
            }else{
                return  ['code'=>1,'msg'=>'申请失败,请稍后重试!'];
            }
        }else{
            $txlog = $this->user->tixian()->where('uid',session('dailiUid'))->order('tx_id', 'desc')->paginate(10);
            // return dump($txlog);
            $data = [
                'yue' => Users::get($this->uid)->money,
                'txlist'=>$txlog
            ];
            return view('yue',$data);
        }
 
    }
}   